package ca.camosun.morsecode.librarycode;

import ca.camosun.morsecode.R;
import android.app.Activity;
import android.bluetooth.*;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class DeviceListActivity extends Activity{ 
	BluetoothAdapter adapter;
	Toast toast; 
	// a request code passed to startActivityforRestult 
	private static final int REQUEST_ENABLE_BT=1;
	

	
	 
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.device_list);
			// try to establish a bluetooth adapter 
			adapter= BluetoothAdapter.getDefaultAdapter();
			toast = Toast.makeText(DeviceListActivity.this,"bluetooth not suported, exiting", 20); 
			validateBluetooth(); 


	}
		
		private boolean validateBluetooth(){
			// froce the bluetoothchek to only run once on startup 
			
			// check if we got an adapter, if not, te device does not support bluetooth 
			if (adapter == null) {
			    toast.show();
			    finish();
			}
			
			// next check that bluetooth is enabled 
			// and request it if it is not 
			
			if (!adapter.isEnabled()) {
			    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
			    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
			}
			doneChecking();
			
			return true; 
		}
		/***
		 * the method that returns the result for startActivity for result 
		 * using it here to determine if the user declined to activate bluetooth 
		 */
		protected void onActivityResult (int requestCode, int resultCode, Intent data){ 
			
			// determine which request we are responding to 
			switch(requestCode){
			
			case REQUEST_ENABLE_BT: 
				if (resultCode== RESULT_CANCELED){
					toast = Toast.makeText(DeviceListActivity.this, "bluetooth declined, app will not run", Toast.LENGTH_LONG);
					toast.show();
					finish(); 
				}
				
			
			}
		}
	
		
		/***
		 * call this when any and all checks have been done
		 * it passes control back to the previos screen 
		 */
		private void doneChecking(){ 
			
			finish();
		}
}